package com.drop.action;

import com.drop.common.Util;
import com.drop.entity.User;
import com.drop.service.UserInformationService;
import com.drop.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by 闄堜笢涓�on 2017/8/4 14:24
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/user")
@Scope("prototype")
public class UserAction  extends ActionSupport {

	@Autowired
	private UserService userService;
	@Autowired
	private UserInformationService useinse;

	/**
	 * 登陆
	 * @return
	 * @throws Exception
	 */
	@Action(value = "login")
	public String login() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		Util.returnJson(response,userService.login(name, name, pwd, request.getSession()));
		return null;
	}

	/**
	 * 登出
	 * @return
	 * @throws Exception
	 */
	@Action(value = "logout",results = {
			@Result(name = "login", location = "/login.jsp")
	})
	public String logout() throws Exception {
		ActionContext.getContext().getSession().clear();
		return "login";
	}

	/**
	 * 注册
	 * @return
	 * @throws Exception
	 */
	@Action(value = "register")
	public String register() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String name = request.getParameter("name");
		String tel = request.getParameter("tel");
		String pwd = request.getParameter("pwd");
		String pwd1 = request.getParameter("pwd1");
		Util.returnJson(response,userService.register(name, tel, pwd, pwd1));
		return null;
	}

	/**
	 * 显示地址
	 * @return
	 */
	@Action(value = "showad",results = {
			@Result(name = "shows", location = "/WEB-INF/person/address.jsp")
	})
	public String showaddress() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		int userid=(int) session.getAttribute("id");
		User user=useinse.getUserById(userid);
		ActionContext.getContext().put("user", user);
		return "shows";
	}


	/**
	 * 编辑
	 * @return
	 */
	@Action(value = "edit",results = {
			@Result(name = "edit", location = "showad",type = "chain")
	})
	public String editaddress() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		int userid=(int) session.getAttribute("id");
		User user=useinse.getUserById(userid);
		String province=request.getParameter("province");
		String city=request.getParameter("city");
		String street=request.getParameter("detail");
		user.setProvince(province);
		user.setCity(city);
		user.setStreet(street);
		useinse.updateUserInfo(user);
		return "edit";
	}
}
